package com.podium.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.auth.utils.SecurityUtils;
import com.podium.generator.entity.GeneratorColumn;
import com.podium.generator.mapper.GeneratorColumnMapper;
import com.podium.generator.service.GeneratorColumnService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 邵文泽
 * @description 代码生成数据库表字段实现类
 * @date 2025/4/16 11:10
 */
@Service
public class GeneratorColumnServiceImpl extends ServiceImpl<GeneratorColumnMapper, GeneratorColumn>  implements GeneratorColumnService {

    @Resource
    private GeneratorColumnMapper generatorColumnMapper;

    @Override
    public IPage<GeneratorColumn> page(Page<GeneratorColumn> page) {
        QueryWrapper<GeneratorColumn> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(GeneratorColumn::getDelFlag, 0);
        return page(page, queryWrapper);
    }

    @Override
    public boolean save(GeneratorColumn generatorColumn) {
        generatorColumn.setDelFlag(0);
        generatorColumn.setCreateBy(SecurityUtils.getUsername());
        generatorColumn.setCreateTime(LocalDateTime.now());
        return super.save(generatorColumn);
    }

    @Override
    public boolean updateById(GeneratorColumn generatorColumn) {
        generatorColumn.setUpdateBy(SecurityUtils.getUsername());
        generatorColumn.setUpdateTime(LocalDateTime.now());
        return super.updateById(generatorColumn);
    }

    @Override
    public List<GeneratorColumn> databaseColumns(String tableName) {
        return generatorColumnMapper.databaseColumns(tableName);
    }

    @Override
    public void removeByTableId(Long tableId) {
        QueryWrapper<GeneratorColumn> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GeneratorColumn::getTableId,tableId);
        generatorColumnMapper.delete(queryWrapper);
    }

    @Override
    public List<GeneratorColumn> listByTableId(Long id) {
        QueryWrapper<GeneratorColumn> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GeneratorColumn::getTableId,id);
        return super.list(queryWrapper);
    }

}
